﻿<?php
require_once(dirname(__FILE__)."/config.php");

if(isset($_GET["id"])) {
	$json = GetJsonData($_GET["id"]);
	if(!empty($json))
	{
//		header("Content-type: text/html; charset=utf-8");
//		header("Cache-Control: no-cache, must-revalidate");
//		header("Expires: Mon, 26 Jul 1976 05:00:00 GMT");
//		header("Pragma: no-cache");
		echo ($json);
	}
	die();
}

function hasChildColumnsBySiteid($siteid)
{
	$dsql = new QFSqli(false);
	$query = "SELECT count(*) FROM `#@__column` where sid='$siteid' ";
	$dsql->SetQuery($query);
	
	$row = $dsql->GetOne();	
	if($row['count(*)'] == 0)
		return false;
	else
		return true;
		
}
function hasChildColumns($cid)
{
	$dsql = new QFSqli(false);
	$dsql->SetQuery("SELECT count(*) FROM `#@__column` where parentid='$cid' ");
	$row = $dsql->GetOne();		
	if($row['count(*)'] == 0)
		return false;
	else 
		return true;
}


function GetJsonData($parentID)
{
	/** res = '[' 
		res += '{ "title": "Node 1", "key": "k1", "isLazy": true },' 
		res += '{ "title": "Node 2", "key": "k2", "isLazy": true },' 
		res += '{ "title": "Node 3", "key": "k3", "isLazy": true }' # no trailing "," at the last line
		res += ']'
	*/
	
	try {		
		$col="";
		$result="";
		if($parentID == "")
		{
			$siteString = '';
						
			$query = "SELECT * FROM `#@__site` order by sortno,updatetime ";
			$dsql = new QFSqli(false);
			$dsql->SetQuery($query);
			
			$dsql->Execute();
			while ($row = $dsql->GetArray())
			{
				$siteString .= ",{ \"title\": \"".$row['sname']."\"";
				$siteString .= ",\"key\":\"".$row['sid']."\"";
				$siteString .= ",\"url\":\"columns.php?sid=".$row['sid']."\"";
				$siteString .= ",\"isFolder\":\"true\" ";
				
				//echo ($row['sname']);
				if(hasChildColumnsBySiteid($row['sid']))
					$siteString .=",\"isLazy\": \"true\" ";
					
				$siteString .= "}";
				
				//echo ($siteString);
			}
			if(!empty($siteString))
				$siteString = "[".substr($siteString,1)."]";
				
			//echo ($siteString ."<br><br>");
			
			$result .= "[{ \"title\": \"网站管理\", \"key\": \"f_site\", \"url\": \"sites.php\", \"isFolder\": true, \"isLazy\": true , \"expand\":true ";
			if(!empty($siteString))
				$result .= ",\"children\": ".$siteString ;
			$result .="}";
			$result .= ",{\"title\": \"系统设置\", \"key\": \"setting\", \"isFolder\": true, \"expand\":true, \"children\": [";
			$result .= "{\"title\": \"用户\", \"key\": \"chgpwd\", \"url\": \"changepwd.aspx\",\"isFolder\": true, \"isLazy\": false },";
			$result .= "{\"title\": \"模板\", \"key\": \"chgpwd\", \"url\": \"changepwd.aspx\",\"isFolder\": true, \"isLazy\": false },";
			$result .= "{\"title\": \"生成\", \"key\": \"chgpwd\", \"url\": \"changepwd.aspx\",\"isFolder\": true, \"isLazy\": false },";
			$result .= "{\"title\": \"密码\", \"key\": \"chgpwd\", \"url\": \"changepwd.aspx\",\"isFolder\": true, \"isLazy\": false }";
			$result .= "]}";
			$result .= "]";
		}
		else 
		{
			if(substr($parentID,0,2) == 's_')
				$query = "SELECT * FROM qf_column where sid='$parentID' order by sortno,updatetime ";
			else
				$query = "SELECT * FROM qf_column where parentid='$parentID' order by sortno,updatetime ";
			
			$dsql = new QFSqli(false);
			$dsql->SetQuery($query);			
			$dsql->Execute();
			while ($row = $dsql->GetArray())
			{
				$result .= ",{ \"title\": \"".$row['cname']."\"";
				$result .= ",\"key\":\"".$row['cid']."\"";
				$result .= ",\"url\":\"articles.php?cid=".$row['cid']."\"";
				$result .= ",\"isFolder\":\"true\" ";
				
				if(hasChildColumns($row['cid']))
					$result .=",\"isLazy\": \"true\" ";
					
				$result .= "}";
			}
			if(!empty($result))
				$result = "[".substr($result,1)."]";
		}
		return $result;
	}catch (Exception $ex)
	{
		echo ($ex->getMessage());
		return "";
	}
}